// components/avatarNickname/avatarNickname.js

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    avatarUrl: '',
    nickname: '',
    isShow:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChooseAvatar(e) {
      const { avatarUrl } = e.detail 
      this.setData({
        avatarUrl,
      })
    },
    nicknameInput(e){
      
      console.log(e)
      this.setData({
        nickname: e.detail.value
      })
    },

    cancel(){
      console.log('cancel')
      this.close()
    },
    confirm(){
      console.log('confirm')
      console.log({
        avatarUrl: this.data.avatarUrl,
        nickname: this.data.nickname
      })
      if(!this.data.nickname){
        wx.showToast({
          title: '请选择昵称',
          icon: 'none',
          duration: 2000
        })
        return
      }

      wx.setStorageSync('avatarUrl', this.data.avatarUrl)
      wx.setStorageSync('nickname', this.data.nickname)

      this.triggerEvent('confirm', {
        avatarUrl: this.data.avatarUrl,
        nickname: this.data.nickname
       })

       this.close()
    },

    open(){
      let a = wx.getStorageSync('avatarUrl')
      let n = wx.getStorageSync('nickname')
      this.setData({
        isShow:true,
        avatarUrl: a ? a : defaultAvatarUrl,
        nickname: n ? n : ''
      })
    },
    close(){
      this.setData({
        isShow:false
      })
    }
  }
})
